﻿using System;
using System.Linq;
using EzDesk.DevHelper.DbMgr.Views;
using EzDesk.Utility.Log;
using EzDesk.Core.Data;

namespace EzDesk.DevHelper.DbMgr.Presenters
{

    public class FeedPresenter
    {
        private IFeedView _view;
        public FeedPresenter(IFeedView view)
        {
            _view = view;
        }

        public void GetFeedInfo(string feedTemplateName)
        {
            LogEx.TRACE("FeedPresenter", "获取Feed");
            using (EzDeskDatabaseContainer db = new EzDeskDatabaseContainer()){

                var feedInfo = db.FeedTemplateSet
                    .Where(p => p.TemplateName == feedTemplateName)
                    .Select(p => new
                    {
                        TemplateName = p.TemplateName,
                        TempleId = p.Id,
                        Title = p.Title,
                        Body = p.Body,
                        Type = p.DefaultTypeValue,
                        Icon = p.TemplateIconName
                    })
                    .Single();

                LogEx.TRACE("FeedPresenter", "获取Feed成功");

                //显示Feed列表
                _view.DisplayFeedInfo(feedInfo);
                
            }


        }

      
      


        public void UpdateTestList(dynamic feedInfo)
        {
            LogEx.TRACE("FeedPresenter", "更新Feed");
            //todo..
            //更新数据库
            using (var db = new EzDeskDatabaseContainer())
            {
                Guid id = feedInfo.TempleId;
                var feed = db.FeedTemplateSet
                    .Where(p => p.Id == id)
                    .SingleOrDefault();

                feed.TemplateName = feedInfo.TemplateName;
                feed.TemplateIconName = feedInfo.TemplateIcon;
                feed.Title = feedInfo.Title;
                feed.Body = feedInfo.Body;
                feed.DefaultTypeValue = feedInfo.Type;

                db.SaveChanges();
                LogEx.TRACE("FeedPresenter", "更新Feed成功");

                //预览Feed
                _view.PreviewFeedInfo();
            }

        }

    }
}
